package com.weitu.task.controller;

import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.task.entity.Task;
import com.weitu.task.service.TaskService;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/5/2.
 */
@Controller
@RequestMapping(value = "task")
public class TaskController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "task/";

    @Autowired
    private TaskService taskService;

    @RequestMapping(value = "/addTask")
    public String addTask() {
        return MODULE_BATH_PATH + "addTask";
    }

    @RequestMapping(value = "/list")
    public String list() {
        return MODULE_BATH_PATH + "list";
    }

    /**
     * 待整改页面
     * @return
     */
    @RequestMapping(value = "/d/list")
    public String dList() {
        return MODULE_BATH_PATH + "dList";
    }

    /**
     * 已整改页面
     * @return
     */
    @RequestMapping(value = "/y/list")
    public String yList() {
        return MODULE_BATH_PATH + "yList";
    }

    /**
     * 待验收页面
     * @return
     */
    @RequestMapping(value = "/dy/list")
    public String dyList() {
        return MODULE_BATH_PATH + "dyList";
    }

    /**
     * 待验收页面
     * @return
     */
    @RequestMapping(value = "/yy/list")
    public String yyList() {
        return MODULE_BATH_PATH + "yyList";
    }

    @ResponseBody
    @RequestMapping("insertData")
    @SystemLog(module = "任务管理", methods = "任务下发")
    @Transactional
    public AjaxResult insertData(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        //boolean accFlag= AccessoryUtil.saveAccerssoryInfo(id);
        boolean boo=taskService.insertData(paramMap);
        if (boo) {
            return success("添加成功");
        }else {
            return fail("添加失败");
        }
    }

    /**
     * 任务列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/taskTable", method = RequestMethod.POST)
    public Object taskTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        User loginUser = getSessionUser();
        String loginUserId = loginUser.getId();
        if (paramMap.get("flag")!=null&&!"".equals(paramMap.get("flag").toString())){
            String flag=paramMap.get("flag").toString();
            if ("zg".equals(flag)){
                paramMap.put("receiveUserId",loginUserId);
            }
            if ("ys".equals(flag)){
                paramMap.put("acceptUserId",loginUserId);
            }
        }
        int totalNum=taskService.selectTaskCount(paramMap);
        DataGridPagination dataGridPagination= DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Task> accounts=taskService.selectAllTask(paramMap);
        Object data=dataGridPagination.flushDataGridData(accounts);
        return data;
    }


    /**
     * 待整改
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectCount")
    public Map<String, Object> selectZg(){
        User loginUser = getSessionUser();
        String loginUserId = loginUser.getId();
        Map<String, Object> paramMapZ = new HashMap<String, Object>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        paramMapZ.put("state",1);
        paramMapZ.put("zgTime",1);
        paramMapZ.put("flag","zg");
        paramMapZ.put("receiveUserId",loginUserId);
        int totalNumZ=taskService.selectTaskCount(paramMapZ);
        resultMap.put("zNum",totalNumZ);
        Map<String, Object> paramMapY = new HashMap<String, Object>();
        paramMapY.put("zgTime",0);
        paramMapY.put("ysTime",1);
        paramMapY.put("flag","ys");
        paramMapY.put("acceptUserId",loginUserId);
        int totalNumY=taskService.selectTaskCount(paramMapY);
        resultMap.put("yNum",totalNumY);
        return resultMap;
    }

    /**
     * 任务列表根据业务Id查询
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/selectByTaskId", method = RequestMethod.POST)
    public List<Task> selectByTaskId(HttpServletRequest request)throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        return taskService.selectByTaskId(paramMap);
    }

    /**
     * 撤销任务
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "任务管理", methods = "撤销任务")
    public AjaxResult deleteById(String id){
        int boo=taskService.deleteById(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    @RequestMapping("info")
    public ModelAndView info(String id,String type) {
        ModelAndView modelAndView = new ModelAndView();
        Task task=taskService.selectById(id);
        modelAndView.addObject("task", task);
        if ("zg".equals(type)){
            modelAndView.setViewName(MODULE_BATH_PATH + "edit");
        }else if ("ys".equals(type)){
            modelAndView.setViewName(MODULE_BATH_PATH + "ysEdit");
        }else {
            modelAndView.setViewName(MODULE_BATH_PATH + "info");
        }
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("updateByIdSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "任务管理", methods = "任务验收提交")
    public AjaxResult updateByIdSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getEditBasicFieldMap(paramMap);
        if (paramMap.get("isYs")!=null&&!"".equals(paramMap.get("isYs").toString())){
            paramMap.put("state",0);
        }
        int boo=taskService.updateById(paramMap);
        if (boo > 0) {
            return success("更新成功");

        } else {
            return fail("更新失败");
        }
    }
}
